(function($, doc) {
	$.init();
	$.ready(function() {
		//位置
		(function(){
			var userPicker = new $.PopPicker();
			userPicker.setData([{
					value: 'ywj',
					text: '客厅'
				}, {
					value: 'aaa',
					text: '主卧室'
				}, {
					value: 'lj',
					text: '次卧室1'
				}, {
					value: 'ymt',
					text: '厨房'
				}, {
					value: 'shq',
					text: '玄关'
				}, {
					value: 'zhbh',
					text: '餐厅'
				}, {
					value: 'zhy',
					text: '设备间'
				}, {
					value: 'gyf',
					text: '走廊'
				}, {
					value: 'zhz',
					text: '次卧'
				}, {
					value: 'gezh',
					text: '卫生间'
				}]);
			var showUserPickerButton = doc.getElementById('position');
			var userResult = doc.getElementById('positionResult');
			showUserPickerButton.addEventListener('tap', function(event) {
				userPicker.show(function(items) {
					userResult.innerText = items[0].text;
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
		})();
		
		//描述
		(function(){
			var userPicker = new $.PopPicker();
			userPicker.setData([{
				value: 'ywj',
				text: '拼缝高低差'
			}, {
				value: 'aaa',
				text: '表面划伤破损'
			}, {
				value: 'lj',
				text: '收口不美观'
			}, {
				value: 'ymt',
				text: '表面污染'
			}, {
				value: 'shq',
				text: '修补明显'
			}]);
			var showUserPickerButton = doc.getElementById('description');
			var userResult = doc.getElementById('descriptionResult');
			showUserPickerButton.addEventListener('tap', function(event) {
				userPicker.show(function(items) {
					userResult.innerText = items[0].text;
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
		})();
		
		//责任单位
		(function(){
			//描述
			var userPicker = new $.PopPicker();
			userPicker.setData([{
				value: 'ywj',
				text: '恒力'
			}, {
				value: 'aaa',
				text: '康宝'
			}, {
				value: 'lj',
				text: '市政'
			}, {
				value: 'ymt',
				text: '四川兴事发门窗有限责任公司'
			}, {
				value: 'shq',
				text: '天力'
			}]);
			var showUserPickerButton = doc.getElementById('responsible');
			var userResult = doc.getElementById('responsibleResult');
			showUserPickerButton.addEventListener('tap', function(event) {
				userPicker.show(function(items) {
					userResult.innerText = items[0].text;
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
		})();
		
		//承建商 整改单位
		(function(){
			//描述
			var userPicker = new $.PopPicker();
			userPicker.setData([{
				value: 'ywj',
				text: '客厅'
			}, {
				value: 'aaa',
				text: '主卧室'
			}, {
				value: 'lj',
				text: '次卧室1'
			}, {
				value: 'ymt',
				text: '厨房'
			}, {
				value: 'shq',
				text: '玄关'
			}, {
				value: 'zhbh',
				text: '餐厅'
			}, {
				value: 'zhy',
				text: '设备间'
			}, {
				value: 'gyf',
				text: '走廊'
			}, {
				value: 'zhz',
				text: '次卧'
			}, {
				value: 'gezh',
				text: '卫生间'
			}]);
			var showUserPickerButton = doc.getElementById('rectification');
			var userResult = doc.getElementById('rectificationResult');
			showUserPickerButton.addEventListener('tap', function(event) {
				userPicker.show(function(items) {
					userResult.innerText = items[0].text;
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
		})();
	});


	
	//初始化滚动条
	mui(".mui-scroll-wrapper").scroll();
	

    //首页
	mui(".mui-bar").on("tap", "a", function () {
	    var btn = ["确定", "取消"];
	    mui.confirm('当前问题未提交，确认要放弃吗？', '', btn, function (e) {
	        if (e.index == 0) {
	            location.href = "home.html";
	        }
	    });
	    
	});


	//提交
	mui(".mui-content").on("tap","#submit",function(){
//				var extras=this.getAttribute("data-id");
//				mui.openWindow({
//					'id': "unit_housing_problem_new.html",
//					'url': "unit_housing_problem_new.html",
//						'extras': extras,
//					'show': {
//						'aniShow': "slide-in-right"
//					},
//					'waiting': {
//						autoShow: false
//					}
//				})
	});
	
	//现场整改
	mui(".mui-content").on("tap","#pass",function(){
		if(this.querySelector("input").getAttribute("checked")){
			this.querySelector("input").removeAttribute("checked")
		}else{
			this.querySelector("input").setAttribute("checked",true)
		};
	})

    //部位
	mui(".mui-content").on("tap", "#part", function () {
	    location.href = "add_problem_part.html";
	    return;
	    mui.openWindow({
	        'id': "add_problem_check.html",
	        'url': "add_problem_check.html",
	        'extras': "",
	        'show': {
	            'aniShow': "slide-in-right"
	        },
	        'waiting': {
	            autoShow: false
	        }
	    })
	});
	
	//检查项
	mui(".mui-content").on("tap", "#check", function () {
	    location.href = "add_problem_check.html";
	    return;
		mui.openWindow({
			'id': "add_problem_check.html",
			'url': "add_problem_check.html",
			'extras': "",
			'show': {
				'aniShow': "slide-in-right"
			},
			'waiting': {
				autoShow: false
			}
		})
	});

    //描述
	mui(".mui-content").on("tap", "#description", function () {
	    location.href = "add_problem_check_desc.html";
	    return;
	    mui.openWindow({
	        'id': "add_problem_check.html",
	        'url': "add_problem_check.html",
	        'extras': "",
	        'show': {
	            'aniShow': "slide-in-right"
	        },
	        'waiting': {
	            autoShow: false
	        }
	    })
	});
	
	//照片
	mui(".mui-content").on("tap","#photo",function(){
		if(mui.os.plus){
			var a = [{
				title: "拍照"
			}, {
				title: "从手机相册选择"
			}];
			plus.nativeUI.actionSheet({
//				title: "修改头像",
				cancel: "取消",
				buttons: a
			}, function(b) {
				switch (b.index) {
					case 0:
						break;
					case 1:
						getImage();
						break;
					case 2:
						galleryImg();
						break;
					default:
						break
				}
			})	
		}
		var a=document.querySelectorAll("textarea");
		a[0].blur();
		return false;
	});
	//删除图片
	mui(".photo").on("tap",".del",function(){
		var _parentElement=this.parentNode.parentNode;
		if(_parentElement){
			_parentElement.removeChild(this.parentNode);
		};
		return false;
	});
	function getImage() {
		var c = plus.camera.getCamera();
		c.captureImage(function(path) {
			plus.io.resolveLocalFileSystemURL(path, function(entry) {
				var s = entry.toLocalURL() + "?version=" + new Date().getTime();
				var temp=parseDom('<div class="mui-text-center">\
					<img class="img" src="'+s+'"/>\
					<img class="del" src="../img/tab_index_focus.png" />\
				</div>')
				var firstNode=document.querySelector(".photo").firstChild;
				document.querySelector(".photo").insertBefore(temp[0],firstNode);
				initImgPreview();
			}, function(path) {
				console.log("读取拍照文件错误：" + e.message);
			});
		}, function(s) {
			console.log("error" + s);
		}, {
			filename: "_doc/head.jpg"
		})
	}

	function galleryImg() {
		plus.gallery.pick(function(path) {
			plus.io.resolveLocalFileSystemURL(path, function(entry) {
				plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
					root.getFile(entry.name, {}, function(file) {
						//文件已存在
						file.remove(function() {
							console.log("file remove success");
							entry.copyTo(root, entry.name, function(e) {
								var e = e.fullPath + "?version=" + new Date().getTime();
								var temp=parseDom('<div class="mui-text-center">\
									<img class="img" src="'+e+'"/>\
									<img class="del" src="../img/tab_index_focus.png" />\
								</div>')
								var firstNode=document.querySelector(".photo").firstChild;
								document.querySelector(".photo").insertBefore(temp[0],firstNode);
								initImgPreview();
							},
							function(e) {
								console.log('copy image fail:' + e.message);
							});
						}, function() {
							console.log("delete image fail:" + e.message);
						});
					}, function() {
						//文件不存在
						entry.copyTo(root, entry.name, function(e) {
							var e = e.fullPath + "?version=" + new Date().getTime();
							var temp=parseDom('<div class="mui-text-center">\
								<img class="img" src="'+e+'"/>\
								<img class="del" src="../img/tab_index_focus.png" />\
							</div>')
							var firstNode=document.querySelector(".photo").firstChild;
							document.querySelector(".photo").insertBefore(temp[0],firstNode);
							initImgPreview();
						},
						function(e) {
							console.log('copy image fail:' + e.message);
						});
					});
				}, function(e) {
					console.log("get _www folder fail");
				})
			}, function(e) {
				console.log("读取拍照文件错误：" + e.message);
			});
		}, function(a) {}, {
			filter: "image"
		})
	};
	
	function defaultImg() {
		if(mui.os.plus){
			plus.io.resolveLocalFileSystemURL("_doc/head.jpg", function(entry) {
				var s = entry.fullPath + "?version=" + new Date().getTime();;
				document.querySelector("head-img").src = s;
				document.querySelector("head-img1").src = s;
			}, function(e) {
				document.querySelector("img.img").src = '../img/tab_index_focus.png';
			})
		}else{
			document.querySelector("img.img").src = '../img/tab_index_focus.png';
		}
	}
	setTimeout(function () {
//		defaultImg();
		setTimeout(function() {
			initImgPreview();
		}, 300);
	},500);
	//图片预览
	function initImgPreview() {
		var imgs = document.querySelectorAll("img.img");
		imgs = mui.slice.call(imgs);
		if (imgs && imgs.length > 0) {
			var slider = document.createElement("div");
			slider.setAttribute("id", "__mui-imageview__");
			slider.classList.add("mui-slider");
			slider.classList.add("mui-fullscreen");
			slider.style.display = "none";
			slider.addEventListener("tap", function() {
				slider.style.display = "none";
				setTimeout(function(){
					var a=document.querySelectorAll("textarea");
						a[0].blur();
				},4) 
				event.stopPropagation();
				event.preventDefault();
				return false;
			});
			slider.addEventListener("touchmove", function(event) {
				event.preventDefault();
			})
			var slider_group = document.createElement("div");
			slider_group.setAttribute("id", "__mui-imageview__group");
			slider_group.classList.add("mui-slider-group");
			imgs.forEach(function(value, index, array) {
				//给图片添加点击事件，触发预览显示；
				value.addEventListener('tap', function() {
					slider.style.display = "block";
					_slider.refresh();
					_slider.gotoItem(index, 0);
				})
				var item = document.createElement("div");
				item.classList.add("mui-slider-item");
				var a = document.createElement("a");
				var img = document.createElement("img");
				img.setAttribute("src", value.src);
				a.appendChild(img)
				item.appendChild(a);
				slider_group.appendChild(item);
			});
			slider.appendChild(slider_group);
			document.body.appendChild(slider);
			var _slider = mui(slider).slider();
		}
	}
	//字符串转换dom
	function parseDom(arg) {
	　　 var objE = document.createElement("div");
	　　 objE.innerHTML = arg;
	　　 return objE.childNodes;
	};
})(mui, document);